#ifdef __QT5_TARGET

#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QtCore/QVariant>

#else

#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>

#endif

#include "VisitManager.h"
#include "CouldNotExecQueryException.h"
#include "CouldNotCastException.h"
#include "NullPointerException.h"
VisitManager::VisitManager(void)
{
	this->visitsByDayCache = new QMap<QTime,Visit*>();
	this->visitByIDCache = new QMap<unsigned int,Visit*>();
	this->unfinishedCache = new QMap<QDateTime, Visit*>();
	this->visits = new QMap<QDateTime,Visit*>();
}

VisitManager::~VisitManager(void)
{
	try
	{
	if(visits != NULL)
	{
		QMap<QDateTime,Visit*>::iterator it = visits->begin();
		while(it != visits->end())
		{
			if(it.value() != NULL)
			{
				delete it.value();
				it.value() = NULL;
			}
			it++;
		}
	
		delete visits;
		visits = NULL;
	}

	if(unfinishedCache != NULL)
	{
		delete unfinishedCache;
		this->unfinishedCache = NULL;
	}

	if(this->visitByIDCache != NULL)
	{
		delete this->visitByIDCache;
		visitByIDCache = NULL;
	}

	if(this->visitsByDayCache != NULL)
	{
		delete this->visitsByDayCache;
		this->visitsByDayCache = NULL;
	}
	}catch(int i)
	{
		Exception e;
		e.setErrorCode(i);
		e.setMessage("Fatal error: Memory corruption while deleting VisitManager");
		throw e;
	}
}



void VisitManager::loadLastVisit()
{

}

void VisitManager::loadVisits(Filter* filter)
{

}

QList<Visit*> VisitManager::getAllUnfinished()
{
	return QList<Visit*>();
}

Visit* VisitManager::getLastVisit()
{
	return NULL;
}

Visit* VisitManager::getLastVisit(unsigned int patient_id)
{
	return NULL;
}

QList<Visit*> VisitManager::getVisits(int pos, int number)
{
	return QList<Visit*>();
}

QList<Visit*> VisitManager::getVisits(QDate date)
{
	return QList<Visit*>();
}

QMap<QTime,Visit*>* VisitManager::getVisitByDayCache()
{
	return this->visitsByDayCache;
}

void VisitManager::deleteVisit(Visit* v)
{
	if(!v)
		return;
	this->visits->remove(v->getVisitDate());
	this->visitByIDCache->remove(v->getVisitId());
	delete v;
}

void VisitManager::loadVisits(QDate date)
{
	/*
	QSqlQuery query;
	QString to = date.toString("yyyy-MM-dd") + "-23-59-59";
	QString from = date.toString("yyyy-MM-dd") + "-00-00-00";
	bool ok = query.prepare("select visit_id,file_Nr,visit_date from visit where visit_date > TO_DATE('" + from +"','yyyy-mm-dd-hh24-mi-ss') and visit_date < to_date('" + to + "','yyyy-mm-dd-hh24-mi-ss')");
	if(!ok)
	{
		Exception e;
		e.setMessage("Could not prepare query");
		throw e;
	}

	
	//query.bindValue(":from");
	//query.bindValue(":to",);
	if(!query.exec())
	{
		CouldNotExecQueryException e;
		e.setMessage("Could not Load Visits. " + query.lastQuery() + query.lastError().text());
		throw e;
	}
	while(query.next())
	{
		unsigned int visit_id = query.value(0).toUInt();
		unsigned int patient_id = query.value(1).toUInt();
		QDateTime visit_date = query.value(2).toDateTime();
		if(!visit_date.isValid())
		{
			CouldNotCastException e;
			e.setMessage("could not cast visit date");
			throw e;
		}
		//bool follow_up = query.value(3).toBool();
		Visit* visit = new Visit(visit_id,patient_id,visit_date,false,false);
		QTime t = visit_date.time();
		if(this->visitsByDayCache == NULL)
		{
			NullPointerException e;
			e.setMessage("Fatal error: visitsByDayCache is null");
			throw e;
		}
		this->visitsByDayCache->insert(t,visit);
	}
	*/
}



QList<Visit*> VisitManager::getVisits(QDate from, QDate to)
{
	return QList<Visit*>();
}

QList<Visit*> VisitManager::getVisits(unsigned int patient_id)
{
	return QList<Visit*>();
}

QList<Visit*> VisitManager::getVisits(Filter *filter)
{
	return QList<Visit*>();
}


QList<Visit*> VisitManager::getFirstVisits()
{
	return QList<Visit*>();
}

QList<Visit*> VisitManager::getLastVisits()
{
	return QList<Visit*>();
}

Visit* VisitManager::getVisit(unsigned int visit_id)
{
	return this->visitByIDCache->value(visit_id,NULL);
}

void VisitManager::addVisit(Visit* visit)
{
	this->visitByIDCache->insert(visit->getVisitId(),visit);
	this->visits->insert(visit->getVisitDate(),visit);
	if(!visit->isFinished())
		this->unfinishedCache->insert(visit->getVisitDate(),visit);
	QTime time = visit->getVisitDate().time();
	this->visitsByDayCache->insert(time,visit);
}


Visit* VisitManager::getNextVisit()
{
	return NULL;
}

Visit* VisitManager::getNextVisit(unsigned int patient_id)
{
	return NULL;
}

QList<Visit*> getNextVisits()
{
	return QList<Visit*>();
}


